﻿namespace WMS6.Web
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web.BaseInfoServiceRef;

    public class Uom : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreated;
        protected DropDownList ddlUomType;
        protected HtmlForm F2101;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblUomDesc;
        protected Label lblUomID;
        protected Label lblUomType;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtUomDesc;
        protected TextBox txtUomID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtUomID.Text = "";
            this.txtUomID.Enabled = true;
            this.chkSysCreated.Checked = false;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C011");
            }
            else if (!string.IsNullOrEmpty(this.txtUomID.Text.Trim()))
            {
                try
                {
                    List<string> uomID = new List<string> {
                        this.txtUomID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(uomID, "UOM") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("UomQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("UomList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtUomID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2250_001");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateUomInfo();
            }
            else
            {
                this.InsertUomInfo();
            }
        }

        private void InsertUomInfo()
        {
            try
            {
                UomInfo uomInfo = new UomInfo {
                    UomID = this.txtUomID.Text.Trim(),
                    UomDescr = this.txtUomDesc.Text.Trim(),
                    SystemCreated = this.chkSysCreated.Checked ? "Y" : "N",
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    UomType = this.ddlUomType.SelectedValue,
                    CreatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(uomInfo, "UOM") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = uomInfo;
                this.txtUomID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlUomType, "UOM");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetUomInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetUomInfo2Form()
        {
            try
            {
                string uomID = base.Request.QueryString["ID"].ToString();
                UomInfo uom = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(uomID, "UOM") as UomInfo;
                base.DataEntityViewState = uom;
                if (uom != null)
                {
                    this.txtUomID.Text = uom.UomID;
                    this.txtUomDesc.Text = uom.UomDescr;
                    this.chkIsActive.Checked = uom.IsActive == "Y";
                    this.chkSysCreated.Checked = uom.SystemCreated == "Y";
                    this.ddlUomType.SelectedValue = uom.UomType;
                    this.txtUomID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateUomInfo()
        {
            try
            {
                UomInfo uomInfo = base.DataEntityViewState as UomInfo;
                uomInfo.UomDescr = this.txtUomDesc.Text.Trim();
                uomInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                uomInfo.UomType = this.ddlUomType.SelectedValue;
                uomInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(uomInfo, "UOM") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

